package cn.xuewenbao.edu.controller;


import cn.xuewenbao.edu.entity.Book;
import cn.xuewenbao.edu.entity.vo.*;
import cn.xuewenbao.edu.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲书表 前端控制器
 * </p>
 *
 * @author mafayu
 * @since 2021-03-30
 */
@RestController
@RequestMapping("/edu/book")
@Api(tags = "讲书表")
@Slf4j
public class BookController {
    @Autowired
    private BookService bookService;

    //获取讲书列表
    @ApiOperation(value = "讲书表-获取讲书列表")
    @GetMapping("getAllBook")
    public List<BookInfoVo> getBookList() {
        List<BookInfoVo> list = bookService.listAllArticle();
        return list;
    }

    //根据用户id获取讲书列表
    @ApiOperation(value = "讲书表-根据用户id获取讲书列表")
    @GetMapping("getIndexBook/{userId}")
    public List<BookIndexVo> getIndexBook(@PathVariable String userId) {
        List<BookIndexVo> list = bookService.listIndexBook(userId);
        return list;
    }

    //根据作者id获取创作讲书列表
    @ApiOperation(value = "讲书表-根据作者id获取创作讲书列表")
    @GetMapping("getCreateBookList/{userId}")
    public Result<List<BookInfoVo>> getOwnBook(@PathVariable String userId) {
        List<BookInfoVo> list = bookService.getCreateBookList(userId);
        Result result = new Result();
        result.setSuccess(true);
        result.setResult(list);
        return result;
    }

    //根据讲书id获取讲书详情-音频
    @ApiOperation(value = "根据讲书id获取讲书详情-音频")
    @GetMapping("getBookAudioDetail/{bookId}/{userId}")
    public Result<BookDetailAudioVo> getBookAudioDetail(@PathVariable String bookId, @PathVariable String userId) {
        Result<BookDetailAudioVo> result = bookService.getBookAudioDetail(bookId, userId);
        return result;
    }

    //根据讲书id获取讲书详情-视频
    @ApiOperation(value = "根据讲书id获取讲书详情-视频")
    @GetMapping("getBookVideoDetail/{bookId}/{userId}")
    public Result<BookDetailVideoVo> getBookVideoDetail(@PathVariable String bookId, @PathVariable String userId) {
        Result<BookDetailVideoVo> result = bookService.getBookVideoDetail(bookId,userId);
        return result;
    }

    //根据讲书id获取讲书购买链接
    @ApiOperation(value = "根据讲书id获取讲书购买链接")
    @GetMapping("getBookBuyLink/{bookId}")
    public Result<BookBuyLink> getBookBuyLink(@PathVariable String bookId) {
        Result<BookBuyLink> result = bookService.getBookBuyLink(bookId);
        return result;
    }

    //添加讲书
    @ApiOperation(value = "添加讲书")
    @PostMapping("addBook")
    public Result<?> addBook(@RequestBody BookPulishVo bookPulishVo) {
        Result<?> result = bookService.addBook(bookPulishVo);
        return result;
    }

    //讲书最终发布
    //修改讲书状态
    @ApiOperation(value = "讲书最终发布")
    @PostMapping("publishBook/{bookId}")
    public Result<Boolean> publishBook(@PathVariable String bookId) {
        Result<Boolean> result = new Result<>();
        Book book = new Book();
        book.setId(bookId);
        book.setStatus("Normal");
        result.setResult(bookService.updateById(book));
        return result;
    }

    //根据id删除讲书
    @ApiOperation(value = "根据id删除讲书")
    @DeleteMapping("deleteBook/{bookId}")
    public Result<?> deleteBook(@PathVariable String bookId) {
        Result<?> result = bookService.deleteBook(bookId);
        return result;
    }

    //根据id和作者id删除讲书
    @ApiOperation(value = "根据id和作者id删除讲书")
    @DeleteMapping("deleteOwnBook/{bookId}/{userId}")
    public Result<?> deleteOwnBook(@PathVariable String bookId,
                                   @PathVariable String userId) {
        Result<?> result = bookService.deleteOwnBook(bookId, userId);
        return result;
    }
}